//
//  TXFTabBar.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/23.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
//创建代理
protocol TXFTabBarDelegate : NSObjectProtocol{

//代理方法:传递一个按钮
    func composeButton()
    
}


//懒加载按钮



class TXFTabBar: UITabBar {
    //weak 的 id代理对象
    
    weak var txfDeledate :TXFTabBarDelegate?
    
//初始化frame
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setuoUI()
    }

required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    
    

    
    //按钮点击事件里设置
@objc private func composeButtonClick(){
    
    //判断代理方法是否执行
    
    txfDeledate?.composeButton()
 
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.composeButton.center.x = self.center.x
        self.composeButton.center.y = self.frame.height / 2
      
        
        let width:CGFloat =  self.frame.width/5
       
        var index:CGFloat = 0
        
        for view in subviews{
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
                view.frame.size.width = width
                view.frame.origin.x = width * index
               
                index++
                if index == 2{
                    index++
                }
            }
        }
    }
    

    
    func setuoUI(){
    //添加
        
        addSubview(composeButton)
   
    }
    
    
    
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        //添加点击事件
        
        
        button.addTarget(self, action: Selector("composeButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
        
    }()


}